package org.seginfo.httpfuzzer;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

/**
 * @author mchiodi
 */
public class Main {
	
	private static Logger logger;
	private static final String DEFAULT_CASES_DIR = ".\\cases\\";

	/**
	 * Params:
	 * -u:{string} Server address
	 * -n:{int} Number of cases to generate
	 * -t:{int} Concurrent threads
	 * -c:{id} Load case
	 * -d:{string} Alternate case repository directory
	 * -m:{boolean} Fuzz HTTP method
	 * -v:{boolean} Fuzz HTTP Version
	 * -s:{boolean} Stop on connection refused
	 * @param args
	 */
	public static void main(String[] args) {
		
		logger = Logger.getLogger(Main.class);
		
		/* Get parameters */
		String address = getParameter("u",args);
		int n = -1;
		try {
			n = Integer.parseInt(getParameter("n",args));
		} catch (Exception e) {
		}
		if (n == -1) {
			n = 1;
		}
		int t = -1;
		try {
			t = Integer.parseInt(getParameter("t",args));
		} catch (Exception e) {
		}
		if (t == -1) {
			t = 1;
		}
		String caseId = getParameter("c",args);
		String directory = getParameter("d",args);
		if (directory == null) {
			directory = DEFAULT_CASES_DIR;
		}
		boolean m = false;
		try {
			m = Boolean.parseBoolean(getParameter("m",args));
		} catch (Exception e) {
		}
		boolean v = false;
		try {
			v = Boolean.parseBoolean(getParameter("v",args));
		} catch (Exception e) {
		}
		boolean s = false;
		try {
			s = Boolean.parseBoolean(getParameter("s",args));
		} catch (Exception e) {
		}
		
		/* Needed parameter */
		if (address == null) {
			System.out.println("Parameter missing.");
			printUsage();
			System.exit(0);
		}
		
		Fuzzer fuzzer = new Fuzzer(address,n,t,caseId,directory,m,v,s);
		fuzzer.execute();
	}
	
	/**
	 * 
	 */
	private static void printUsage() {
		System.out.println("Usage:");
		System.out.println();
		System.out.println("Synopsis:");
		System.out.println();
		System.out.println("\tsifuzzer -u:{address} (OPTION)*");
		System.out.println();
		System.out.println("Options:");
		System.out.println();
		System.out.println("\t-n:{int}\tNumber of cases to be generated (default: 1)");
		System.out.println("\t-t:{int}\tNumber of threads to use (default: 1)");
		System.out.println("\t-d:{string}\tChange cases store directory (default: ./cases/)");
		System.out.println("\t-m:{boolean}\tFuzz HTTP method field (default: false)");
		System.out.println("\t-v:{boolean}\tFuzz HTTP version field (default: false)");
		System.out.println("\t-s:{boolean}\tStop case generation on connection refused (default: false)");
		System.out.println("\t-c:{idstring}\tLoads by ID a previuosly generated case and executes it");
		System.out.println();
		System.out.println("Examples:");
		System.out.println();
		System.out.println("\tsifuzzer -u:localhost -n:1000 -t:5 -m:true -s:true");
		System.out.println();
		System.out.println("\tsifuzzer -u:localhost -c:11967918874370.024654570733505077");
		System.out.println();
	}
	
	/**
	 * @param parameter
	 * @param args
	 * @return
	 */
	private static String getParameter(String parameter, String[] args) {
		Pattern pattern = Pattern.compile("-" + parameter + ":.*");
		for (int i = 0; i < args.length; i++) {
			Matcher matcher = pattern.matcher(args[i]);
			if (matcher.matches()) {
				return args[i].substring(3);
			}
		}
		return null;
	}
}
